package no.uib.hplims.views.peptides;

import org.vaadin.appfoundation.persistence.facade.FacadeFactory;

import no.uib.hplims.models.peptideinformation.PeptideInformation;
import no.uib.hplims.tools.Icons;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Label;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Runo;

public class CommentView extends VerticalLayout implements ClickListener {

	private static final long serialVersionUID = 7977559055546303541L;

	private PeptideInformation peptideInformation;
	private Label richText;
	private Button b;

	private final RichTextArea editor = new RichTextArea();

	public CommentView(PeptideInformation pi) {
		this.setCaption("Comments");
		this.setMargin(true, true, false, true);
		this.setSpacing(true);

		this.peptideInformation = pi;
		
		Label l = new Label("Comment:");
		l.setStyleName(Runo.LABEL_SMALL);
		addComponent(l);
		
		richText = new Label(peptideInformation.getComment());
		richText.setContentMode(Label.CONTENT_XHTML);
		addComponent(richText);

		b = new Button("Edit");
		b.setIcon(Icons.editIcon16);
		b.addListener(this);
		addComponent(b);
		
		editor.setWidth("100%");

	}

	public void buttonClick(ClickEvent event) {
		
		if (event.getButton().getCaption().equalsIgnoreCase("Edit")) {
            editor.setValue(richText.getValue());
            replaceComponent(richText, editor);
            b.setCaption("Save");
            b.setIcon(Icons.saveIcon16);
        } else {
            richText.setValue(editor.getValue());
            peptideInformation.setComment((String) editor.getValue());
            FacadeFactory.getFacade().store(peptideInformation);
            replaceComponent(editor, richText);
            b.setCaption("Edit");
            b.setIcon(Icons.editIcon16);
        }
	}

}
